package com.example.travel.Controller;

import com.example.travel.Service.MessageServer;
import com.example.travel.domain.Messages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 功能：
 * 作者：lln
 * 日期：2025/3/17 12:56
 */
@RestController
@RequestMapping("/api/messages")
public class MessageController {

    @Autowired
    private MessageServer messageService;

    /**
     * 保存消息
     * @param fromUser 发送者用户名
     * @param toUser 接收者用户名
     * @param messageContent 消息内容
     * @return 返回保存结果
     */
    @GetMapping("/save")
    public ResponseEntity<String> saveMessage(@RequestParam String fromUser,
                                              @RequestParam String toUser,
                                              @RequestParam String messageContent) {
        messageService.saveMessage(fromUser, toUser, messageContent);
        return ResponseEntity.ok("消息已保存");
    }

    /**
     * 获取离线消息
     * @param toUser 接收者用户名
     * @return 返回离线消息列表
     */
    @GetMapping("/offline")
    public ResponseEntity<List<Messages>> getOfflineMessages(@RequestParam String toUser) {
        List<Messages> offlineMessages = messageService.getOfflineMessages(toUser);
        return ResponseEntity.ok(offlineMessages);
    }

    /**
     * 标记消息为已读
     * @param messageId 消息ID
     * @return 返回操作结果
     */
    @PutMapping("/read/{messageId}")
    public ResponseEntity<String> markMessageAsRead(@PathVariable int messageId) {
        messageService.markMessageAsRead(messageId);
        return ResponseEntity.ok("消息已标记为已读");
    }
}